<extend name="base"/>

<block name="main">
    <div class="set-box">
        <div class="sb-nr">
            <label class="sbn-l">联系人：</label>
            <input placeholder="请输入联系人姓名" placeholder-style="color:#aaa;" class="sbn-wb username" value="{$data.username}"></input>
            <div class="clear"></div>
        </div>

        <div class="sb-nr">
            <label class="sbn-l">联系方式：</label>
            <input type="number" placeholder="请输入手机号码" placeholder-style="color:#aaa;" class="sbn-wb mobile" value="{$data.mobile}"></input>
            <div class="clear"></div>
        </div>

        <div class="sb-nr">
            <label class="sbn-l">证件号码：</label>
            <input type="" placeholder="请输入法人身份证号码" placeholder-style="color:#aaa;" class="sbn-wb idcard" value="{$data.idcard}"></input>
            <div class="clear"></div>
        </div>

        <div class="sb-nr">
            <label class="sbn-l" style="width:auto;">上传身份证正面照片</label>
            <div class="sbn-r">
                <if condition="$data['card_front_path']">
                    <img src='{$data.card_front_path}' mode="aspectFill"></img>
                    <else />
                    <img src='__HELPER_IMG__/img_sctp.png' mode="aspectFill" id="upload_card_front"></img>
                </if>
                <input type="hidden" name="card_front" id="card_front" value="{$data['card_front']}"/>
                <input type="file" id="card_front_file" style="display: none">
            </div>
            <div class="clear"></div>
        </div>

        <div class="sb-nr">
            <label class="sbn-l" style="width:auto;">上传身份证反面照片</label>
            <div class="sbn-r">
                <if condition="$data['card_behind_path']">
                    <img src='{$data.card_behind_path}' mode="aspectFill"></img>
                    <else />
                    <img src='__HELPER_IMG__/img_sctp.png' mode="aspectFill" id="upload_card_behind"></img>
                </if>

                <input type="hidden" name="card_behind" id="card_behind" value="{$data['card_behind']}"/>
                <input type="file" id="card_behind_file" style="display: none">
            </div>
            <div class="clear"></div>
        </div>

        <div class="sb-nr">
            <label class="sbn-l" style="width:auto;">上传手持身份证照片</label>
            <div class="sbn-r">
                <if condition="$data['hold_card_path']">
                    <img src='{$data.hold_card_path}' mode="aspectFill"></img>
                    <else />
                    <img src='__HELPER_IMG__/img_sctp.png' mode="aspectFill" id="upload_hold_card"></img>
                </if>
                <input type="hidden" name="hold_card" id="hold_card" value="{$data['hold_card']}"/>
                <input type="file" id="hold_card_file" style="display: none">
            </div>
            <div class="clear"></div>
        </div>

        <if condition="$data['status'] == 2 || !$data['status']">
            <button class="sb-btn" onclick="submit()">提交申请</button>
        </if>
        <if condition="$data['status'] == 0">
            <button class="sb-btn" onclick="submit()" style="background: #CCC">审核中...</button>
        </if>
    </div>
</block>

<block name="js">
    <script>
        $("#upload_card_front").click(function (){
            $("#card_front_file").trigger("click");
        })
        $('#card_front_file').change(function (){
            var pic = $(this)[0].files[0];
            var formData = new FormData();
            var url="{:U('File/uploadPicture',array('session_id'=>session_id(), 'max_kb' => 200))}";
            formData.append('download', pic);
            $.ajax({
                url:url,
                type:"post",
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                success:function(data){
                    if(data.status == 1){
                        //上传成功
                        var id = data.id;
                        var path = data.path;

                        $('#card_front').val(id);
                        $('#upload_card_front').attr('src', path);
                    }else{
                        layer.msg(data.info);
                    }
                }
            });
        });

        $("#upload_card_behind").click(function (){
            $("#card_behind_file").trigger("click");
        })
        $('#card_behind_file').change(function (){
            var pic = $(this)[0].files[0];
            var formData = new FormData();
            var url="{:U('File/uploadPicture',array('session_id'=>session_id(), 'max_kb' => 200))}";
            formData.append('download', pic);
            $.ajax({
                url:url,
                type:"post",
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                success:function(data){
                    if(data.status == 1){
                        //上传成功
                        var id = data.id;
                        var path = data.path;

                        $('#card_behind').val(id);
                        $('#upload_card_behind').attr('src', path);
                    }else{
                        layer.msg(data.info);
                    }
                }
            });
        });

        $("#upload_hold_card").click(function (){
            $("#hold_card_file").trigger("click");
        })
        $('#hold_card_file').change(function (){
            var pic = $(this)[0].files[0];
            var formData = new FormData();
            var url="{:U('File/uploadPicture',array('session_id'=>session_id(), 'max_kb' => 200))}";
            formData.append('download', pic);
            $.ajax({
                url:url,
                type:"post",
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                success:function(data){
                    if(data.status == 1){
                        //上传成功
                        var id = data.id;
                        var path = data.path;

                        $('#hold_card').val(id);
                        $('#upload_hold_card').attr('src', path);
                    }else{
                        layer.msg(data.info);
                    }
                }
            });
        });

        function submit()
        {
            var url = "{:U('applyInstitution')}"
            var uid = "{$Think.session.uid}";
            var username = $('.username').val();
            var mobile = $('.mobile').val();
            var idcard = $('.idcard').val();
            var card_front = $('#card_front').val();
            var card_behind = $('#card_behind').val();
            var hold_card = $('#hold_card').val();

            $.showLoading('数据提交中');

            post_ajax(url, {
                uid: uid,
                username: username,
                mobile: mobile,
                idcard: idcard,
                card_front: card_front,
                card_behind: card_behind,
                hold_card: hold_card
            }, function (res){
                layer.msg(res.msg);
                if(res.result == 1){
                    setTimeout(function (){
                        history.go(-1);
                    },1000)
                }
            })

        }
    </script>

</block>